<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入手机号码"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>



    <el-table v-loading="loading" :data="userIntegralList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center">
        <template slot-scope="scope">
          {{ scope.$index+1 }}
        </template>
      </el-table-column>
      <el-table-column label="用户名称" align="center" prop="userName" />
      <el-table-column label="手机号码" align="center" prop="phonenumber" />
      <el-table-column label="积分" align="center" prop="integral" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['userIntegral:userIntegral:edit']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户积分管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="user" :rules="rules" label-width="80px">
        <el-form-item label="用户名称" prop="userName">
          <el-input v-model="user.userName" placeholder="" disabled="true" />
        </el-form-item>
        <el-form-item label="用户积分" prop="oldIntegral">
          {{user.oldIntegral}}
        </el-form-item>
        <el-form-item label="积分变化" prop="integral">

          <el-input-number style="width: 380px"
                           v-model="user.integral"
                           controls-position="right"
          >
          </el-input-number>
        </el-form-item>
        <el-form-item label="修改原因" prop="updateReason">
          <el-input v-model="user.updateReason" placeholder=""  />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUserIntegral, getUserIntegral, delUserIntegral, addUserIntegral, updateUserIntegral } from "@/api/userIntegral/userIntegral";

export default {
  name: "UserIntegral",
  data() {
    return {
      user:{
        updateReason:"",
        integral:0
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户积分管理表格数据
      userIntegralList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName:null,
        phonenumber:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        prizeId: [
          { required: true, message: "兑换商品id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户积分管理列表 */
    getList() {
      this.loading = true;
      listUserIntegral(this.queryParams).then(response => {
        this.userIntegralList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        exchangeRecordsId: null,
        prizeId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        userName:null,
        phonenumber:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.exchangeRecordsId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户积分管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row)
      this.user=JSON.parse(JSON.stringify(row));
      this.user.integral=0;
      let parse = JSON.parse(JSON.stringify(row));
      this.user.oldIntegral=parse.integral;
      this.open=true;
      this.title = "修改用户积分";

    },
    /** 提交按钮 */
    submitForm() {

      updateUserIntegral(this.user).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.open = false;
        this.getList();
      })


    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const exchangeRecordsIds = row.exchangeRecordsId || this.ids;
      this.$modal.confirm('是否确认删除用户积分管理编号为"' + exchangeRecordsIds + '"的数据项？').then(function() {
        return delUserIntegral(exchangeRecordsIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('userIntegral/userIntegral/export', {
        ...this.queryParams
      }, `userIntegral_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
